﻿using D.B.Data.API.Applications.Command.LeaveCommand;
using D.B.Data.Domain;
using D.B.Data.ErrorCode;
using D.B.Data.Infrastructuse;
using MediatR;

namespace D.B.Data.API.Applications.CommandHandler.LeaveCommandHandler
{
    public class DeptBoosUpStateCommandHandler : IRequestHandler<DeptBoosUpStateCommand, APIResult<int>>
    {
        private readonly IBaseRepository<WorkflowModel> baseRepository;
        private readonly IBaseRepository<DeptModel> deptRepository;

        public DeptBoosUpStateCommandHandler(IBaseRepository<WorkflowModel> baseRepository, IBaseRepository<DeptModel> deptRepository)
        {
            this.baseRepository = baseRepository;
            this.deptRepository = deptRepository;
        }

        public Task<APIResult<int>> Handle(DeptBoosUpStateCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> aPIResult = new APIResult<int>();
            var res = baseRepository.GetAll().FirstOrDefault(s => s.Id == request.Id)!;
            var DeptBoosId = deptRepository.GetAll().FirstOrDefault(s => s.Id == res.DeptId)!.BoosId;
            if (DeptBoosId != request.UserId)
            {
                aPIResult.Code=EnumCode.File;
                aPIResult.Msg = "不是审批人";
            }
            else
            {
                if(request.IsResult=="同意")
                {
                    res.State = true;
                    res.IsResult = request.IsResult;
                   
                }
                else
                {
                    res.State = true;
                    res.IsResult = request.IsResult;
                    res.ResultDesc = request.ResultDesc;
                }
                baseRepository.Update(res);
                aPIResult.Code = EnumCode.Ok;
                aPIResult.Msg = "审批成功";
            }

            return Task.FromResult(aPIResult);
        }
    }
}
